const fs = require('fs')
const path = require('path')

function resolve(dir) {
  return path.resolve(__dirname, dir)
}
function capitalize(str) {
  return str.replace(/\B([A-Z])/g, '-$1').toLowerCase()
}
const join = path.join
function getEntries(path) {
  const files = fs.readdirSync(resolve(path))
  const entries = files.reduce((ret, item) => {
    const itemPath = join(path, item)
    const isDir = fs.statSync(itemPath).isDirectory()
    if (isDir) {
      if (fs.existsSync(join(itemPath, 'index.js'))) {
        item = capitalize(item)
        ret[item] = resolve(join(itemPath, 'index.js'))
      }
    } else {
      const [name] = item.split('.')
      ret[name] = resolve(`${itemPath}`)
    }
    return ret
  }, {})
  console.log(entries)
  return entries
}

const buildConfig = {
  publicPath:'./',
  // configureWebpack: {
  //   devtool: 'source-map'
  // },
  lintOnSave: true,
  productionSourceMap: false,
  chainWebpack: (config) => {
    //忽略的打包文件
    config.externals({
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'vuex': 'Vuex',
      'axios': 'axios',
      'element-ui': 'ELEMENT',
      'xe-utils': 'XEUtils',
      echarts: 'echarts'
    })
    const entry = config.entry('app')
    entry
      .add('babel-polyfill')
      .end()
    entry
      .add('whatwg-fetch')
      .end()
    entry
      .add('classlist-polyfill')
      .end()
    if(process.env.NODE_ENV === 'development' )
      entry
        .add('@/mock')
        .end()
  },
  transpileDependencies: [
    'wujie',  // 指定对第三方组件也进行babel-polyfill处理
    'wujie-vue2',
    'sock-js','sockjs-client','element-ui'
 ],
  devServer: {
    // 端口配置
    port: 1888,
    // 反向代理配置
    proxy: {
      '/api': {
        target: 'http://localhost:8800',
        ws: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    }
  }
}

const libConfig = {
  outputDir: 'public/build',
  productionSourceMap: false,
  css: {
    sourceMap: true,
    extract: {
      filename: 'style/[name].css'
    }
  },
  configureWebpack: {
    entry: {
      // ...getEntries('plugins/package')
      'index': resolve(join('plugins/package', 'index.js'))
    },
    output: {
      filename: '[name].js',
      libraryTarget: 'umd',
      libraryExport: 'default',
      library: '[name]',
      umdNamedDefine: true,
      globalObject: 'typeof self !== \'undefined\' ? self : this'
    }
  },
  chainWebpack: config => {
    config.externals({
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'vuex': 'Vuex',
      'axios': 'axios',
      'element-ui': 'ELEMENT',
      'xe-utils': 'XEUtils',
      echarts: 'echarts'
    })

    config.module
      .rule('js')
      .use('babel')
      .loader('babel-loader')
      .tap(options => {
        return options
      })
      .end()

    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()
    config.optimization.delete('splitChunks')
    config.plugins.delete('copy')
    config.plugins.delete('html')
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')
    config.plugins.delete('hmr')
    config.entryPoints.delete('app')
  }
}
console.log(process.argv)
module.exports = process.argv[4] === 'designer' ? libConfig : buildConfig